﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using Web.Api.Server.Models;
using Web.Api.Server.ViewModels;
using Web.Api.Server.ViewModels.Shops;

namespace Web.Api.Server.Controllers.Shops
{
    [SwaggerTag("前端商品分类接口")]
    [Route("api/shops/{shopId}/[controller]")]
    [ApiController]
    public class GoodsCategoriesController : ControllerBase
    {
        private readonly ApplicationDbContext _dbContext;

        public GoodsCategoriesController(ApplicationDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        [HttpGet]
        [ProducesResponseType(typeof(ResponseViewModel<IEnumerable<GoodsCategory>>), 200)]
        public async Task<IActionResult> Get(int shopId)
        {
            var categories = await _dbContext.GoodsCategories
                .Where(gc => gc.ShopId == shopId)
                .ToListAsync();
            return ApiResponse.Ok(categories);
        }

        [HttpPost]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Post(int shopId, GoodsCategoriesPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId)
            {
                var category = new GoodsCategory
                {
                    Name = viewModel.Name,
                    Index = viewModel.Index,
                    Remark = viewModel.Remark,
                    ShopId = viewModel.ShopId,
                };
                await _dbContext.GoodsCategories.AddAsync(category);
                var result = await _dbContext.SaveChangesAsync();

                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }

        [HttpPut("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Put(int shopId, int id, GoodsCategoriesPostViewModel viewModel)
        {
            if (shopId == viewModel.ShopId && id == viewModel.Id)
            {
                var category = await _dbContext.GoodsCategories
                    .Where(c => c.Id == viewModel.Id)
                    .FirstAsync();
                if (category != null)
                {
                    category.Name = viewModel.Name;
                    category.Index = viewModel.Index;
                    category.Remark = viewModel.Remark;
                    category.UpdatedAt = DateTime.Now;

                    await _dbContext.GoodsCategories.UpdateAsync(category);
                    var result = await _dbContext.SaveChangesAsync();
                    return ApiResponse.Ok(result);
                }
            }

            return ApiResponse.NotFound();
        }

        [HttpDelete("{id}")]
        [ProducesResponseType(typeof(ResponseViewModel<int>), 200)]
        public async Task<IActionResult> Delete(int shopId, int id)
        {
            var category = await _dbContext.GoodsCategories
                .Where(c => c.ShopId == shopId && c.Id == id)
                .FirstAsync();
            if (category != null)
            {
                _dbContext.GoodsCategories.Remove(category);
                var result = await _dbContext.SaveChangesAsync();
                if (result > 0)
                {
                    // 从相关商品中删除分类数据
                    await _dbContext.GoodsSet
                        .Where(g => g.GoodsCategoryId == category.Id)
                        .ToUpdate()
                        .Set(g => g.GoodsCategoryId, null)
                        .Set(g => g.UpdatedAt, DateTime.Now)
                        .ExecuteAffrowsAsync();
                }
                return ApiResponse.Ok(result);
            }

            return ApiResponse.NotFound();
        }
    }
}